<template>
	<view>
		<city-select
			@cityClick="cityClick"
			@districtClick="districtClick"
			:formatName="formatName"
			:activeCity="activeCity"
			:hotCity="hotCity"
			:countyDistrict="countyDistrict"
			:obtainCitys="obtainCitys"
			:isSearch="true"
			:districtName ="districtName"
			ref="citys"
		></city-select>
	</view>
</template>

<script>
import {cityCityId, cityHot} from '@/api/city'
import { commonStore } from '@/stores/common'
let app = getApp()
import citys from './citys.js'
console.log(citys.length)

import citySelect from './components/city-select/city-select'
// import { getArea,getHotCity,getCity } from '@/api/index'
export default {
	data() {
		return {
			//需要构建索引参数的名称（注意：传递的对象里面必须要有这个名称的参数）
			formatName: 'title',
			//当前城市
			activeCity: {
				id: 1,
				title: '南京市'
			},
			//县区
			countyDistrict: [
				{
					id: 0,
					title: '全区'
				},
				{
					id: 1,
					title: '福田区'
				}
			],
			//热门城市
			hotCity: [
				{
					id: 0,
					title: '南京市'
				},
				{
					id: 1,
					title: '南京市'
				}
			],
			//显示的城市数据
			obtainCitys: [
				{
					id: 0,
					title: '南京'
				},
				{
					id: 1,
					title: '北京'
				},
				{
					id: 2,
					title: '天津'
				},
				{
					id: 3,
					title: '东京'
				}
			],
			districtName:''
		}
	},
	components: {
		citySelect
	},
	onLoad() {
		//修改数据格式
		this.formatName = 'cityName'
		//修改当前城市
    const CommonStore =commonStore()
    if(CommonStore.cityInfo.cityName){
      this.activeCity = {
        cityName: CommonStore.cityInfo.cityName,
        cityCode: CommonStore.cityInfo.cityCode
      }
    }else {
      this.activeCity = {
        cityName: '深圳市',
        cityCode:"440300"
      }
    }
    cityHot().then(res=>{
      this.hotCity =res
    })

    this.getDistrict(this.activeCity.cityCode)
		//修改构建索引数据
		this.obtainCitys = citys
	},
	methods: {
		cityClick(item) {
      const CommonStore =commonStore()
			console.log(item,'cityClick---item')
			this.activeCity = item
			app.globalData.cityName = item.cityName
			app.globalData.cityCode = item.cityCode
			app.globalData.districtName = ''
			this.districtName = ''
      this.getDistrict(item.cityCode)
      this.getLoaction(item.cityName)
      CommonStore.setCityInfo({
        cityCode: item.cityCode,
        cityName:item.cityName
      })

			// if(item.cityCode=="810000"||item.cityCode=="820000"||item.cityCode=="110000"||item.cityCode=="120000"||item.cityCode=="310000"||item.cityCode=="500000"){
			// 	this.getCity(item.cityCode)
			// }else{
			// 	this.getDistrict(item.cityCode)
			// }

		},
    getLoaction(searchVal){
      var QQMapWX = require('../../static/qqmap-wx-jssdk.min.js');
     let qqmapsdk = new QQMapWX({
      	key: 'LKJBZ-ILRL6-5GTSI-E443R-NEXIE-6QF2R'
      });
      qqmapsdk.search({
        keyword: searchVal,
        success: function(res) {
          console.log('res',res)
        }
      })
    },
		districtClick(item) {
			console.log(item,'districtClick---item')
			app.globalData.districtCode = item.cityCode
			app.globalData.districtName = item.cityName
			this.districtName = item.cityName

		},
		getDistrict(id){
			this.countyDistrict = []
			let that = this
      cityCityId(id).then(res=>{
        this.countyDistrict =res
        console.log(this.countyDistrict,'res---区县')

      })
			// // 引入腾讯地图SDK
			// var QQMapWX = require('../../static/qqmap-wx-jssdk.min.js');
			// var qqmapsdk;
			// // 实例化API核心类
			// qqmapsdk = new QQMapWX({
			// 	key: 'LKJBZ-ILRL6-5GTSI-E443R-NEXIE-6QF2R'
			// });
			// // 获取区县数据
			// qqmapsdk.getDistrictByCityId({
			//     // 传入对应省份ID获得城市数据，传入城市ID获得区县数据,依次类推
			//     id: id, //如：深圳是'440300'
			//     success: function(result_) {
			// 		console.log('对应城市ID下的区县数据：', result_.result[0]);
			// 		result_.result[0].forEach(item=>{
			// 			that.countyDistrict.push({
			// 				cityName: item.fullname,
			// 				cityCode: item.id
			// 			})
			// 		})
			//     },
			//     fail: function(error_) {
			//         console.error(error_);
			//     }
			// });
			// getArea({cityId:id}).then(res=>{
			// 	console.log(res,'res---区县')
			// 	app.globalData.district = res.data
			// 	res.data.forEach(item=>{
			// 		that.countyDistrict.push({
			// 			cityName: item.areaName,
			// 			cityCode: item.areaId
			// 		})
			// 	})
			// })
		},
		getCity(id){
			this.countyDistrict = []
			let that = this
			getCity({provinceId:id}).then(res=>{
				console.log(res,'res---市')
				app.globalData.district = res.data
				res.data.forEach(item=>{
					that.countyDistrict.push({
						cityName: item.cityName,
						cityCode: item.cityId
					})
				})
			})
		}
	}
}
</script>

<style>
	page{
		background-color: #fff;
	}
</style>
